using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using Metro.Admin.BusinessEntities;
using Microsoft.Practices.EnterpriseLibrary.Data;   

namespace Metro.Admin.DataAccess
{
    partial class AdminDataSource
    {
        public List<Profile> GetProfiles()
        {
            List<Profile> profiles = null;
            string roleId = string.Empty;
            string roleName = string.Empty;
            string description = string.Empty;

            try
            {
                profiles = new List<Profile>();
                string sqlCommand = "SELECT [RoleId],[RoleName],[Description] FROM [Roles]";
                DbCommand dbCommand = metroDb.GetSqlStringCommand(sqlCommand);
                using (IDataReader dataReader = metroDb.ExecuteReader(dbCommand))
                {
                    while (dataReader.Read())
                    {
                        roleId = dataReader["RoleId"].ToString();
                        roleName = dataReader["RoleName"].ToString();
                        description = dataReader["Description"].ToString();

                        profiles.Add(new Profile(roleId, roleName, description));
                    }
                }
            }
            catch(Exception ex)
            {
                throw ex;            
            }

            return profiles;
        }
        public void UpdateProfile(Profile profile)
        {
            Guid idProfile = new Guid(profile.Id);
            try
            {
                string sqlCommand = "UPDATE [Roles] SET [RoleName] = @RoleName, [Description] = @Description WHERE [RoleId] = @RoleId";
                                    
                DbCommand dbCommand = metroDb.GetSqlStringCommand(sqlCommand);

                metroDb.AddInParameter(dbCommand, "@RoleName", DbType.String, profile.Name);
                metroDb.AddInParameter(dbCommand, "@Description", DbType.String, profile.Description);
                metroDb.AddInParameter(dbCommand, "@RoleId", DbType.Guid, idProfile);
                metroDb.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void CreateProfile(Profile profile)
        {
            try
            {
                string sqlCommand = "INSERT INTO [Roles] ([RoleName],[Description]) VALUES (@RoleName,@Description)";

                DbCommand dbCommand = metroDb.GetSqlStringCommand(sqlCommand);

                metroDb.AddInParameter(dbCommand, "@RoleName", DbType.String, profile.Name);
                metroDb.AddInParameter(dbCommand, "@Description", DbType.String, profile.Description);
                metroDb.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void DeleteProfile(Profile profile)
        {
            Guid idProfile = new Guid(profile.Id);
            try
            {
                string sqlCommand = "DELETE FROM [Roles] WHERE [RoleId] = @RoleId";
                DbCommand dbCommand = metroDb.GetSqlStringCommand(sqlCommand);

                metroDb.AddInParameter(dbCommand, "@RoleId", DbType.String, idProfile);
                metroDb.ExecuteNonQuery(dbCommand);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public Profile GetProfile(string idProfile)
        {
            Guid id= new Guid(idProfile);
            Profile retProfile=null;

            try
            {
                string sqlCommand = "SELECT [RoleId],[RoleName],[Description] FROM [Roles] WHERE [RoleId] = @RoleId";
                DbCommand dbCommand = metroDb.GetSqlStringCommand(sqlCommand);
                metroDb.AddInParameter(dbCommand, "@RoleId", DbType.String, id);
               
                using (IDataReader dataReader = metroDb.ExecuteReader(dbCommand))
                {
                    while (dataReader.Read())
                    {
                        retProfile = new Profile();
                        retProfile.Id = idProfile;
                        retProfile.Name = dataReader["RoleName"].ToString();
                        retProfile.Description = dataReader["Description"].ToString();
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return retProfile; 
        }
    }
}
